package cn.yoyo.service;

import cn.yoyo.dao.BlogDao;
import cn.yoyo.dao.LikeDao;
import cn.yoyo.entity.Blog;
import cn.yoyo.entity.Like;
import cn.yoyo.tools.MySession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LikeServiceImpl implements LikeService{
    @Autowired
    LikeDao likeDao;
    @Autowired
    BlogDao blogDao;

    @Override
    public void add(String blogId) {
        likeDao.add(new Like(blogId,MySession.getUserId()));
        Blog blog = blogDao.findById(blogId);
        Map<String,Object> map = new HashMap<>();
        map.put("blogId",blogId);
        map.put("likes",blog.getLikes()+1);
        blogDao.updateSum(map);
    }

    @Override
    public void delete(Like like) {
        likeDao.delete(like.getId());
        Blog blog = blogDao.findById(like.getBlogId());
        if (blog.getLikes()>0){
            Map<String,Object> map = new HashMap<>();
            map.put("blogId",blog.getId());
            map.put("likes",blog.getLikes()-1);
            blogDao.updateSum(map);
        }
    }
}
